package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.dto.DailyTrainCreateDTO;
import com.lovelyj.trip_by_train.dto.DailyTrainDto;
import com.lovelyj.trip_by_train.entity.DailyRemainTicket;
import com.lovelyj.trip_by_train.entity.DailyTrain;
import com.lovelyj.trip_by_train.entity.Train;
import com.lovelyj.trip_by_train.service.ArriveService;
import com.lovelyj.trip_by_train.service.CoachService;
import com.lovelyj.trip_by_train.service.DailyRemainTicketService;
import com.lovelyj.trip_by_train.service.DailyTrainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/dailyTrain")
public class DailyTrainController {

    @Autowired
    private DailyTrainService dailyTrainService;

    @Autowired
    private CoachService coachService;

    @Autowired
    private ArriveService arriveService;
    @Autowired
    private DailyRemainTicketService dailyRemainTicketService;

    @GetMapping("/page")
    public R<Page<DailyTrainDto>> page(HttpServletRequest request, Integer page, String  date, String trainNum, String startStation, String endStation){
        log.info(date);
        LocalDate date1=null;
        if (date!=null) {
            date = date.substring(0, 10);
            date1 = LocalDate.parse(date);
        }
        log.info(String.valueOf(date1));
        Page<DailyTrainDto> page1=dailyTrainService.selectDailyTrainDtoList(page,date1,trainNum,startStation,endStation);
        return R.success(page1);
    }


    @Transactional
    @PutMapping("/create")
    public R<String> create(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        if (map.get("dates")==null) {
            return R.error("请选择日期！");
        } else if (map.get("trainNum")==null) {
            return R.error("请选择车次");
        }
        log.info(map.toString());
        String trainNum = (String) map.get("trainNum");
        List<String> dates = (List<String>) map.get("dates");

        log.info(dates.toString());
        DailyTrain dailyTrain;
        for (String date : dates) {
            dailyTrain = new DailyTrain();
            dailyTrain.setDate(LocalDate.parse(date.substring(0, 10)).plusDays(1));
            dailyTrain.setTrainNum(trainNum);
            LambdaQueryWrapper<DailyTrain> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DailyTrain::getTrainNum,trainNum);
            queryWrapper.eq(DailyTrain::getDate,dailyTrain.getDate());
            if (dailyTrainService.count(queryWrapper) > 0) {
                return R.error("该班次"+dailyTrain.getDate()+"已存在");
            }

            dailyTrainService.saveWithSeatAndTickets(dailyTrain);

        }
        return R.success("success");
    }

    @Transactional
    @PostMapping("/deleteByIds")
    public R<String> deleteByIds(HttpServletRequest request,@RequestBody List<Long> selectedIds){

        log.info("deleteByIds...");

        for (Long selectedId : selectedIds) {
            dailyTrainService.removeWithSeatAndTickets(selectedId);
        }
        return R.success("删除成功");
    }

    @DeleteMapping
    public R<String> deleteById(HttpServletRequest request,Long id){
        log.info("deleteById..."+id);
        dailyTrainService.removeWithSeatAndTickets(id);
        return R.success("删除成功");
    }



}
